package com.icode.dao.impl;

import com.icode.core.model.house.ExchangePool;
import com.icode.core.model.house.MyHouseStatus;
import com.icode.core.model.house.ProductType;
import com.icode.core.model.user.User;
import com.icode.core.model.user.UserInfo;
import com.icode.dao.ExchangeDao;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-6-8
 * Time: 下午3:26
 * To change this template use File | Settings | File Templates.
 */
@Repository("exchangeDao")
public class ExchangeDaoImpl extends EntityDaoImpl implements ExchangeDao {


    @Override
    public List<ExchangePool> findExchangePoolByHouseBase(String userInfo) {
        String hql = "from " + ExchangePool.class.getName() + " a where a.myHouse.houseInfo.buildingType.houseBase.productType=:productType and a.myHouse.userInfo.guid !=:userInfo";
        Query query = currentSession().createQuery(hql);
        query.setParameter("productType", ProductType.HOUSEBASE);
        query.setParameter("userInfo", userInfo);
        return query.list();
    }

    @Override
    public List<ExchangePool> findExchangePoolByApartment(String userInfo) {
        String hql = "from " + ExchangePool.class.getName() + " a where a.myHouse.houseInfo.buildingType.houseBase.productType=:productType and a.myHouse.userInfo.guid !=:userInfo";
        Query query = currentSession().createQuery(hql);
        query.setParameter("productType", ProductType.APARTMENT);
        query.setParameter("userInfo", userInfo);
        return query.list();
    }

    @Override
    public List<ExchangePool> findExchangePoolByUserInfo(UserInfo userInfo) {
        String hql = "from " + ExchangePool.class.getName() + " a where a.myHouse.userInfo=:userInfo and a.myHouse.myHouseStatus=:myHouseStatus and a.myHouse.usable=true";
        Query query = currentSession().createQuery(hql);
        query.setParameter("userInfo", userInfo);
        query.setParameter("myHouseStatus", MyHouseStatus.CHANGING);
        return query.list();
    }

    @Override
    public List<ExchangePool> findExchangePoolNotUserInfo(UserInfo userInfo) {
        String hql = "from " + ExchangePool.class.getName() + " a where a.myHouse.userInfo !=:userInfo and a.myHouse.myHouseStatus=:myHouseStatus and a.myHouse.usable=true";
        Query query = currentSession().createQuery(hql);
        query.setParameter("userInfo", userInfo);
        query.setParameter("myHouseStatus", MyHouseStatus.CHANGING);
        return query.list();
    }

    @Override
    public UserInfo findUserInfoByUser(User user) {
        String hql = "from " + UserInfo.class.getName() + " a where a.user=:user";
        Query query = currentSession().createQuery(hql);
        query.setParameter("user", user);
        List<UserInfo> list = query.list();
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public List<ExchangePool> findAllExchangePools(String userInfo) {
        String hql = "from " + ExchangePool.class.getName() + " a where a.myHouse.userInfo.guid !=:userInfo";
        Query query = currentSession().createQuery(hql);
        query.setParameter("userInfo", userInfo);
        return query.list();
    }

    @Override
    public List<ExchangePool> findExchageHouseByUsername(String username) {
        String hql = "from " + ExchangePool.class.getName() + " a where a.myHouse.userInfo.username=:username";
        Query query = currentSession().createQuery(hql);
        query.setParameter("username", username);
        return query.list();
    }
}
